package utils;

import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

public class StringUtils {
	public static final String DECIMAL_EQUALS_ZERO = "^[0-9]+(.[0]{2})$";


	public static boolean isEmpty(List list) {
		if (list != null && list.size() > 0) {
			return false;
		} else {
			return true;
		}
	}
	/**
	 * 澶勭悊绌哄瓧绗︿覆
	 * 
	 * @param str
	 * @return String
	 */
	public static String doEmpty(String str) {
		return doEmpty(str, "");
	}

	/**
	 * 澶勭悊绌哄瓧绗︿覆
	 * 
	 * @param str
	 * @param defaultValue
	 * @return String
	 */
	public static String doEmpty(String str, String defaultValue) {
		if (str == null || str.equalsIgnoreCase("null")
				|| str.trim().equals("") || str.trim().equals("锛嶈閫夋嫨锛�")) {
			str = defaultValue;
		} else if (str.startsWith("null")) {
			str = str.substring(4, str.length());
		}
		return str.trim();
	}

	/**
	 * 璇烽�夋嫨
	 */
	final static String PLEASE_SELECT = "璇烽�夋嫨...";

	public static boolean notEmpty(Object o) {
		return o != null && !"".equals(o.toString().trim())
				&& !"null".equalsIgnoreCase(o.toString().trim())
				&& !"undefined".equalsIgnoreCase(o.toString().trim())
				&& !PLEASE_SELECT.equals(o.toString().trim());
	}

	public static boolean isEmpty(Object o) {
		return o == null || "".equals(o.toString().trim())
				|| "null".equalsIgnoreCase(o.toString().trim())
				|| "undefined".equalsIgnoreCase(o.toString().trim())
				|| PLEASE_SELECT.equals(o.toString().trim());
	}

	public static boolean num(Object o) {
		int n = 0;
		try {
			n = Integer.parseInt(o.toString().trim());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		if (n > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean decimal(Object o) {
		double n = 0;
		try {
			n = Double.parseDouble(o.toString().trim());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		if (n > 0.0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 缁橨ID杩斿洖鐢ㄦ埛鍚�
	 * 
	 * @param Jid
	 * @return
	 */
	public static String getUserNameByJid(String Jid) {
		if (isEmpty(Jid)) {
			return null;
		}
		if (!Jid.contains("@")) {
			return Jid;
		}
		return Jid.split("@")[0];
	}

	/**
	 * 缁欑敤鎴峰悕杩斿洖JID
	 * 
	 * @param jidFor
	 *            鍩熷悕//濡俛hic.com.cn
	 * @param userName
	 * @return
	 */
	public static String getJidByName(String userName, String jidFor) {
		if (isEmpty(jidFor) || isEmpty(jidFor)) {
			return null;
		}
		return userName + "@" + jidFor;
	}

	/**
	 * 缁欑敤鎴峰悕杩斿洖JID,浣跨敤榛樿鍩熷悕ahic.com.cn
	 * 
	 * @param userName
	 * @return
	 */
	public static String getJidByName(String userName) {
		String jidFor = "ahic.com.cn";
		return getJidByName(userName, jidFor);
	}

	/**
	 * 鏍规嵁缁欏畾鐨勬椂闂村瓧绗︿覆锛岃繑鍥炴湀 鏃� 鏃� 鍒� 绉�
	 * 
	 * @param allDate
	 *            like "yyyy-MM-dd hh:mm:ss SSS"
	 * @return
	 */
	public static String getMonthTomTime(String allDate) {
		return allDate.substring(5, 19);
	}

	/**
	 * 鏍规嵁缁欏畾鐨勬椂闂村瓧绗︿覆锛岃繑鍥炴湀 鏃� 鏃� 鍒� 鏈堝埌鍒嗛挓
	 * 
	 * @param allDate
	 *            like "yyyy-MM-dd hh:mm:ss SSS"
	 * @return
	 */
	public static String getMonthTime(String allDate) {
		return allDate.substring(5, 16);
	}

	public static String getUid(String from) {
		if (notEmpty(from)) {
			int index = from.lastIndexOf("@");
			if (index != -1) {
				return from.substring(0, index);
			}
		}
		return null;
	}

	public static String nullConvert(String source) {
		if (source == null || "null".equals(source)) {
			return "";
		} else {
			return source;
		}
	}

	public static String avatarReplace50(String source) {
		if (source == null || "null".equals(source)
				|| !source.contains("_50_50")) {
			return source;
		} else {
			return source.replace("_50_50", "_120_120");
		}
	}

	public static boolean decimalEqualsZero(String str) {
		Pattern pattern = Pattern.compile(DECIMAL_EQUALS_ZERO);
		return pattern.matcher(str).matches();
	}

	/**
	 * 鑾峰彇UUID
	 * 
	 * @return 32UUID灏忓啓瀛楃涓�
	 */
	public static String gainUUID() {
		String strUUID = UUID.randomUUID().toString();
		strUUID = strUUID.replaceAll("-", "").toLowerCase();
		return strUUID;
	}
}
